package com.onpositive.wiki3.db.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.onpositive.wiki3.db.IRedirectAccess;
import com.onpositive.wiki3.db.ITextContentAccess;
import com.onpositive.wiki3.db.ITitleMapper;
import com.onpositive.wiki3.db.IWikiDB;

public class RedirectAccessImpl implements IRedirectAccess {

	int[] redirects;
	IWikiDB db;

	public RedirectAccessImpl(IWikiDB db, File pfile) {
		this.db=db;
		File ri = new File(pfile, "redirects.dat");
		if (!ri.exists()) {
			build(db, ri);
		}
		int l = (int) (ri.length() / 4);
		try {
			DataInputStream ss = new DataInputStream(new BufferedInputStream(
					new FileInputStream(ri)));
			redirects = new int[l];
			for (int a = 0; a < l; a++) {
				redirects[a] = ss.readInt();
			}
			ss.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private void build(IWikiDB db2, File pfile) {
		ITextContentAccess contentAccessor = db2.getContentAccessor();
		ITitleMapper titleMapper = db2.getTitleMapper();
		int size = contentAccessor.size();
		try {
			DataOutputStream ss = new DataOutputStream(
					new BufferedOutputStream(new FileOutputStream(pfile)));
			for (int a = 0; a < size; a++) {
				String text = contentAccessor.getTextContent(a);
				String string = "#redirect [[";				
				if (text.toLowerCase().startsWith(string)) {
					text = text.substring(string.length());
					int p = text.indexOf("]]");
					if (p==-1){
						ss.writeInt(-1);			
						continue;
					}
					text = text.substring(0, p);
					text = text.replace(' ', '_');
					int dot = text.indexOf('#');
					if (dot!=-1){
						text=text.substring(0,dot);
					}
					int id = titleMapper.getId(text);
					if (id==-1){
						int[] ffm=titleMapper.getFuzzyMatches(text);
						if (ffm.length>0){
							id=ffm[0];
						}				
						else{
							if (!text.toLowerCase().startsWith("wikipedia_talk")){
								System.out.println(text);	
							}
							
						}
					}
					ss.writeInt(id);					
				}
				else{
					ss.writeInt(-1);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int getRedirect(int localId) {
		try{
			if (localId>=redirects.length){
				return -1;
			}
			return redirects[localId];
		}
		catch( Exception e )
		{
			e.printStackTrace() ;
			return -5 ;
		}
	}

	@Override
	public String normalizeTitle(String title) {
		ITitleMapper titleMapper = db.getTitleMapper();
		int id = titleMapper.getId(title);
		if (id == -1) {
			return title;
		}
		try{
		int redirect = getRedirect(id);
		if (redirect == -1) {
			return title;
		}
		if (redirect == -5) {
			return null;
		}
		String title2 = titleMapper.getTitle(redirect);
		return title2;
		}catch (Exception e) {
			System.out.println(db.getContentAccessor().getTextContent(id));
		}
		return title;
	}

}
